package com.lzxuni.modules.productdisplay;


import com.alibaba.fastjson.JSON;
import com.lzxuni.common.utils.QRCodeUtils;
import com.lzxuni.common.utils.R;
import com.lzxuni.common.utils.StringUtils;
import com.lzxuni.common.utils.UuidUtil;
import com.lzxuni.common.utils.web.RequestUtils;
import com.lzxuni.modules.common.controller.BaseController;
import com.lzxuni.modules.common.entity.FileEntity;
import com.lzxuni.modules.common.entity.PageData;
import com.lzxuni.modules.common.entity.PageParameter;
import com.lzxuni.modules.common.entity.Tree;
import com.lzxuni.modules.common.service.FileEntityService;
import com.lzxuni.modules.dataAcquisition.service.ChainService;
import com.lzxuni.modules.enterprise.service.EnterpriseInfoService;
import com.lzxuni.modules.productdisplay.entity.ProductDisplay;
import com.lzxuni.modules.productdisplay.service.ProductDisplayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.logging.SimpleFormatter;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author llj
 * @since 2019-06-27
 */
@RestController
@RequestMapping("/baseProductDisplay")
public class ProductDisplayController extends BaseController {
    @Autowired
    private ProductDisplayService productDisplayService;
    @Autowired
    private ChainService chainService;
    @Autowired
    private FileEntityService fileEntityService;
    @Autowired
    private EnterpriseInfoService enterpriseInfoService;
    //商品展示列表跳转
    @RequestMapping("/Index")
    public ModelAndView list() {
        ModelAndView mv = new ModelAndView("/productdisplay/index");
        return mv;
    }
    //商品展示列表
    @RequestMapping("/GetList")
    public Object GetList(String pagination, ProductDisplay productDisplay, String Keyword) throws SQLException {
        PageParameter pageParameter = JSON.parseObject(pagination, PageParameter.class);
        productDisplay.setProductName(Keyword);
        PageData pageData = getPageData(productDisplayService.queryPage(pageParameter, productDisplay));
        return R.ok().put("data", pageData);

    }
    //商品展示新增 修改 跳转
    @RequestMapping("/Form")
    public ModelAndView insert(String keyValue) throws Exception {
        ModelAndView mv = new ModelAndView("/productdisplay/Insert");
        return mv;
    }
    //商品展示删除
    @RequestMapping("/DeleteForm")
    public Object delete(String keyValue) {
        productDisplayService.removeById(keyValue);
        return R.ok("删除成功");
    }

   //所属企业查询
    @RequestMapping("/GetSelectTree")
    public Object GetSelectTree(String nature)  {
        List<Tree> trees = enterpriseInfoService.getTreeSelectTree(nature);
        return R.ok().put("data",trees);
    }
    //追溯链条查询
    @RequestMapping("/GetSelectTree1")
    public Object GetSelectTree1(String nature)  {
        List<Tree> trees = chainService.getTreeSelectTree(nature);
        return R.ok().put("data",trees);
    }
    //商品展示新增 修改
    @RequestMapping("/SaveForm")
    public Object insertDo(FileEntity fileEntity,String keyValue, String demopics,String demopics1,String demopics2, String demofiles,ProductDisplay productDisplay) throws Exception{
        SimpleDateFormat formatter = new SimpleDateFormat( "yyyyMMdd-hhmmss" ) ;
        if(StringUtils.isEmpty(keyValue)){
            String ywId = UuidUtil.get32UUID();
            productDisplay.setCreateTime(new Date());
            productDisplay.setTraceabilityCode(formatter.format(new Date()));
            productDisplay.setYwId(ywId);
            String contents = productDisplay.getTraceabilityCode();
            String fileName = UuidUtil.get32UUID();// 生成的图片文件名
            // 生成二维码
            BufferedImage qrcode = QRCodeUtils.createQRCode(contents);
            //添加图片表二维码信息
            fileEntityService.insert2(ywId,contents,fileName);
            //打印二维码图片
            boolean result = QRCodeUtils.generateQRCodeToPath(qrcode,fileName);
            System.out.println("执行结果" + result);
            if(!StringUtils.isEmpty(demopics) && !"&amp;nbsp;".equals(demopics)) {
                fileEntityService.insert(demopics.replace("&quot;", "\""), ywId, "tp", "demopic", null);
            }
            if(!StringUtils.isEmpty(demopics1) && !"&amp;nbsp;".equals(demopics1)) {
                fileEntityService.insert(demopics1.replace("&quot;", "\""), ywId, "tp", "demopic1", null);
            }
            productDisplayService.save(productDisplay);
            return R.ok("保存成功");
        }else{
            fileEntityService.deleteByYwId(keyValue);
            if(!StringUtils.isEmpty(demopics) && !"&amp;nbsp;".equals(demopics)) {
                fileEntityService.insert(demopics.replace("&quot;", "\""), keyValue, "tp", "demopic", null);
            }
            if(!StringUtils.isEmpty(demopics1) && !"&amp;nbsp;".equals(demopics1)) {
                fileEntityService.insert(demopics1.replace("&quot;", "\""), keyValue, "tp", "demopic1", null);
            }
          /*  if(!StringUtils.isEmpty(demopics2) && !"&amp;nbsp;".equals(demopics2)) {
                fileEntityService.insert(demopics2.replace("&quot;", "\""), keyValue, "tp", "demopic2", null);
            }*/
            productDisplay.setYwId(keyValue);
            productDisplayService.updateById(productDisplay);
            return R.ok("修改成功");
        }

    }
    //富文本回显
    @RequestMapping("GetEntity")
    public Object  GetEntity(String keyValue) {
        ProductDisplay productDisplay = productDisplayService.getById(keyValue);
        return R.ok().put("data", productDisplay);
    }
    //图片回显
    @RequestMapping("/Update")
    public ModelAndView update(String ywId,String keyValue) throws Exception{
        ModelAndView mv = new ModelAndView("/productdisplay/Update");
        FileEntity fileBeanCustom = new FileEntity();
        fileBeanCustom.setYwId(ywId);
        fileBeanCustom.setYwType("demopic");
        mv.addObject("picNum", fileEntityService.queryNumByFileEntity(fileBeanCustom));
        FileEntity fileBeanCustom1 = new FileEntity();
        fileBeanCustom1.setYwId(ywId);
        fileBeanCustom1.setYwType("demopic1");
        mv.addObject("picNum1", fileEntityService.queryNumByFileEntity(fileBeanCustom1));
        return mv;
    }
    @RequestMapping("/qrcode")
    public ModelAndView qrcode(String ywId, String keyValue, HttpServletRequest request) throws Exception{
        ModelAndView mv = new ModelAndView("/productdisplay/qrcode");
        FileEntity fileBeanCustom2 = new FileEntity();
        System.out.println(ywId);
        fileBeanCustom2.setYwId("s"+ywId);
        fileBeanCustom2.setYwType("demopic2");
        mv.addObject("picNum2", fileEntityService.queryListByFileEntity(fileBeanCustom2));
        String ipAddr = RequestUtils.getIpAddr(request);
        mv.addObject("ipAddr", ipAddr);

        return mv;
    }
}

